package edu.richmond.is.webservices.files2exist;

import java.io.File;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;

import edu.richmond.is.webservices.util.FilesInFolderList;

/**
 * This program provides a way of auditing
 * the files that f2e would try to process
 * for a supplied configuration.
 *  
 */
public class EnumerateFileCandidates {

	public static void main(String[] args) throws Exception {
		//
		// Explicitly shut off logging for log4j
		// unless explicitly turned on with a 
		// pathed configuration file
		//
		if(System.getProperty("log4j.configuration") == null) {
			Properties props = new Properties();
			props.setProperty("log4j.rootLogger", "OFF");
			PropertyConfigurator.configure(props);
		}
		//
		// Make sure that a config file was passed
		//
		if(args.length != 1) {
			System.err.println("<javacmd> /path/to/f2xConfig.xml");
			System.exit(-1);
		}
		

		File config = new File(args[0]);
		//
		// Make sure the config file exists
		//
		if(!config.exists()) {
			System.err.println("Non-existent config specified: " + config);
			System.exit(-2);
		}
		//
		// Initialize the appconfig (internal class to F2E)
		//
		AppConfig configObj = new AppConfig(config.toString());
		//
		// Get the folders setting list
		//
		List<Folder2ExistSettings> dirListObj = configObj.getFolderSettingsArrayList();
		//
		// Begin enumeration messages
		//
		System.out.println("Begin Enumerating Files for config: " + config.toString());
		//
		// Process each directory in the config
		//
		for (Folder2ExistSettings folderSettings: dirListObj) {
			//
			// Print the header for the directory
			//
			System.out.println("  Enumerating files under: " + folderSettings.getFolderName() + ", " +
		         (folderSettings.isRecurse()?"":"non-") + "recursive");
			
			if(folderSettings.getFilePattern() != null &&
					folderSettings.getFilePattern().trim().length() > 0) {
				System.out.println("   Filtering FilePattern: " + folderSettings.getFilePattern());
			}
			
			//
			// Enumerate the files tagged for processing by the configuration
			// and print to the console.
			//
			FilesInFolderList fileList = new FilesInFolderList(folderSettings.getFolderName(), folderSettings.isRecurse(), folderSettings.getFilePattern());
			List<File> fileArrayList = fileList.getfileArrayList();
			for(File f: fileArrayList) {
				System.out.println("    Match: " + f);
			}
		}
		//
		// Output complete
		//
		System.out.println("End Enumerating Files");

	}

}
